from flask import Flask, jsonify, request
import uuid
import json

app = Flask(__name__)

# 加载 JSON 数据库文件
def load_database(file_path):
    with open(file_path, 'r', encoding='utf-8') as f:
        return json.load(f)

# 模拟医院数据
hospitals = load_database('./DBFiles/hospital_db.json')

# 模拟科室数据
departments = load_database('./DBFiles/department_db.json')

# 模拟医生数据
doctors = load_database('./DBFiles/doctor_db.json')

# 模拟预约数据
appointments = load_database('./DBFiles/appointment_db.json')


@app.route('/get_hospitals', methods=['POST'])
def get_hospitals():
    return jsonify(hospitals)


@app.route('/get_departments', methods=['POST'])
def get_departments():
    # 由于没有具体的实现流程，现在手动添加模拟数据
    # hospital_id = request.args.get('hospital_id')
    hospital_id = "h1"
    if hospital_id in departments:
        return jsonify(departments[hospital_id])
    else:
        return jsonify({"errCode": "no_hospital", "message": "未找到您指定的医院"}), 200


@app.route('/get_doctors', methods=['POST'])
def get_doctors():
    # 由于没有具体的实现流程，现在手动添加模拟数据
    # department_id = request.args.get('department_id')
    department_id = "d1"
    if department_id in doctors:
        return jsonify(doctors[department_id])
    else:
        return jsonify({"errCode": "no_department", "message": "未找到您指定的科室"}), 200


@app.route('/book_appointment', methods=['POST'])
def book_appointment():
    data = request.get_json()
    # 由于没有具体的实现流程，现在手动添加模拟数据
    # doctor_id = data.get('doctor_id')
    # appointment_time = data.get('appointment_time')
    # patient_info = data.get('patient_info')
    doctor_id = "doc1"
    appointment_time = "2025-02-05T10:00:00"
    patient_info = {
        "patient_name": "小红",
        "patient_age": 25,
        "patient_gender": "女"
    }

    # 简单模拟检查号源，实际需更复杂逻辑
    if doctor_id and appointment_time and patient_info:
        appointment_id = str(uuid.uuid4())
        appointments[appointment_id] = {
            "doctor_id": doctor_id,
            "appointment_time": appointment_time,
            "patient_info": patient_info
        }
        return jsonify(appointments[appointment_id])
    else:
        return jsonify({"errCode": "invalid_info", "message": "提交的预约信息无效"}), 200


if __name__ == '__main__':
    app.run(debug=True, host='0.0.0.0', port=5004)
